package com.imooc.utils;

import com.drore.www.logbeansecret.AesUtil;
import com.imooc.bean.PageAndPageSizeBean;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;


/**
 * @Description //TODO sixbing
 * @Author sixbing
 * @Date 2018/11/3  13:44
 **/
public class RequestUtil {
    /**
     * 获得参数值
     * get request parameter value
     *
     * @param request      request
     * @param key          parameter key
     * @param defaultValue default value
     * @return int value
     */
    public static int getParamesInt(HttpServletRequest request, String key, int defaultValue) {
        String VALUE = request.getParameter(key);
        return getStringToInt(VALUE, key, defaultValue);
    }

    /**
     * 获得请求头参数值
     * get request head parameter value
     *
     * @param request request
     * @return int value
     */
    public static int[] getHeadParamesInt(HttpServletRequest request) {
        int[] pageBean = new int[2];
        try {
            String line;
            BufferedReader reader = request.getReader();
            while ((line = reader.readLine()) != null) {
                PageAndPageSizeBean pageSizeBean = JsonUtils.jsonToPojo(line, PageAndPageSizeBean.class);
                pageBean[0] = pageSizeBean.getPage();
                pageBean[1] = pageSizeBean.getPageSize();
            }
        } catch (Exception e) {
            pageBean[0] = 1;
            pageBean[1] = 10;
            System.out.println(",已经赋予默认值:" + pageBean[0] + " ---- " + pageBean[1]);
            e.printStackTrace();
        }
        return pageBean;
    }

    /**
     * 获得请求头参数值
     * get request head parameter value
     *
     * @param request request
     * @return int value
     */
    public static <T> T getHeadParames(HttpServletRequest request, T obj) {
        T bean = null;
        try {
            String line;
            BufferedReader reader = request.getReader();
            while ((line = reader.readLine()) != null) {
//                System.out.println("-----------" + line);
                line = new String(AesUtil.getInstance().decrypt(line, "0123456789012345", "0123456789012345"));
                System.out.println("-----------" + line);
                bean = (T) JsonUtils.jsonToPojo(line, obj.getClass());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bean;
    }

    /**
     * 获得请求头参数值
     * get request head parameter value
     *
     * @param request request
     * @return int value
     */
    public static String getHeadParames(HttpServletRequest request) {
        StringBuilder bean = new StringBuilder();
        try {
            String line;
            BufferedReader reader = request.getReader();
            while ((line = reader.readLine()) != null) {
                bean.append(line + "\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bean.toString();
    }

    /**
     * 字符串转数字
     * String  to  int
     *
     * @param mValue       string   Value
     * @param key          key
     * @param defaultValue default value
     * @return int value
     */
    public static int getStringToInt(String mValue, String key, int defaultValue) {
        int keyValue;
        try {
            if (StringUtils.isEmpty(mValue)) {
                keyValue = defaultValue;
                System.out.println(key + "的值为:" + mValue + ",已经赋予默认值:" + defaultValue);
            } else {
                keyValue = Integer.parseInt(mValue);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            keyValue = defaultValue;
        }
        return keyValue;
    }

    /**
     * 请求头参数转成map键值对形式
     *
     * @param request 请求信息
     * @return map
     */
    public static Map<String, String> getHeadersInfo(HttpServletRequest request) {
        Map<String, String> map = new HashMap<String, String>();
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            map.put(key, value);
        }
        return map;
    }
}
